<?php

include( 'handy_user_class.inc.php' );

class user_class_extended extends user_class 
{
    // Add your application specific functions here
	
	public $TABLE_COLUMNS = array(
								'username' 		=> 'Username',
								'password' 		=> 'Password',
								'id'			=> 'User_id',
				
								'firstname' 	=> 'First_name',
								'lastname' 		=> 'Last_name',
								'birthdate'		=> 'Birth_date',
								'gender'		=> 'Gender',
								'address'		=> 'Address',
								'city'			=> 'City',
								'country'		=> 'Country',
								'website'		=> 'Website',
								'email'			=> 'Email',
								'avatar'		=> 'Avatar',
								'registerdate'	=> 'Register_date',
								'active'		=> 'Active',
								'comments'		=> 'Comments',
								'penalties'		=> 'Penalties',
								'lastlogin'		=> 'Last_login',
								'balance'		=> 'Balance',
								'rankpoints'	=> 'Rank_points',
								'usertype'		=> 'User_type',
								'language'		=> 'Language',
								);
	public $ROW_TO_DATABASE = array();
	
	function get_value ( $column ) //returneaza valoarea dintr-o anumita coloana din db
	{
    	if ( isset( $this->ROW_FROM_DATABASE[ $this->TABLE_COLUMNS[ $column ] ] ) )
    	{
    		return $this->ROW_FROM_DATABASE[ $this->TABLE_COLUMNS[ $column ] ];
    	}
    	else
    	{
    		$this->get_database_row();
    		return $this->ROW_FROM_DATABASE[ $this->TABLE_COLUMNS[ $column ] ];
    	}
	}
	
	function set_value ( $column, $value ) //seteaza valoarea unei coloane care trebuie schimbata in db cu submit_values()
	{
		$this->ROW_TO_DATABASE[ $this->TABLE_COLUMNS[ $this->DB_LINK->db_real_escape_string($column) ] ] = $this->DB_LINK->db_real_escape_string( $value );
	}
	
	function submit_values() //MODIFICA date in baza de date setate cu set_value()
	{
		$this -> get_database_row(true);
		
		$sql = " UPDATE `$this->TABLE_NAME` SET  "; 
		
		$update = false; //ca sa stiu daca sa rulez query, si imi spune primul element ca sa pun virgula
		
		foreach ( $this->ROW_TO_DATABASE as $key=>$value )
		{
			if ($value != $this->ROW_FROM_DATABASE[ $key ] ) 
			{
				if ( $update ) 
				{
					$sql .= ', ';
				}
				$sql .= " `$key`='$value'";
				$update = true;
			}
		}
		
		if ( $update )
		{
			$sql.=" WHERE `".$this->TABLE_COLUMNS['id']."`='$this->USER_ID' ";
	
			$result = $this->DB_LINK->db_query( $sql );
			
			if ( empty( $result['error']) ) 
			{
				return true;
			}
			else 
			{
				return $result['error'];
			}
		}
		
		return 'NOTHING_TO_SUBMIT';
	}

	function login( $User_name, $Password, $Remember_with_cookie = false, $Load_info = true )	
	{
		$result = parent::login ( $User_name, $Password, $Remember_with_cookie, $Load_info);
		if ( $result ) 
		{
			$this->set_value ( 'lastlogin', date( "Y-m-d H:i:s", time() ) );
			$this->submit_values();
		}
		else return $result;
	}
	
	function new_user( $Username, $Password )
	{
		$result = parent::new_user( $Username, $Password );
		
		if ( (int) $result ) 
		{
			$this->USER_ID = $result;
			
			$this->set_value ( 'registerdate', date( "Y-m-d H:i:s", time() ) );
			$this->submit_values();
			
			return $result;
		}
		else 
		{
			return $result;
		}
		
	}

	function change_password( $old_password, $new_password )
	{
        if ( empty( $old_password ) )
        {
        	$this->ERROR = 'OLD_PASSWORD_IS_EMPTY';
            return false;
        }
        if ( empty( $new_password ) )
        {
        	$this->ERROR = 'NEW_PASSWORD_IS_EMPTY';
            return false;
        }
		
        switch ( strtolower( $this->ENCRYPTION ) )
        {
            case 'md5':
                $hashed_password = md5( $old_password );
                break;
            case 'sha1':
                $hashed_password = sha1( $old_password );
                break;
        }
		
        if ( $hashed_password == $this->get_value('password') )
		{
			switch ( strtolower( $this->ENCRYPTION ) )
			{
				case 'md5':
					$this->set_value('password', md5( $new_password ) );
					break;
				case 'sha1':
					$this->set_value('password', sha1( $new_password ) );
					break;
			}
			return $this->submit_values();
		}
		else
		{
			$this->ERROR = 'WRONG_PASSWORD';
			return false;
		}
	}

	function get_user_list_like( $search_string ) //returneaza un array cu useri cautati dupa username cu LIKE (id, username, rank_points, country)
	{
		$search = $this->DB_LINK->db_real_escape_string( $search_string );

        $sql = sprintf( "
        		SELECT 
        			`%s`,
        			`%s`,
        			`%s`,
        			`%s` 
        		FROM 
        			`%s` 
        		WHERE 
        			`%s` 
        		LIKE '%%%s%%'
        		", 
        		$this->TABLE_COLUMNS['id'], 
        		$this->TABLE_COLUMNS['username'], 
        		$this->TABLE_COLUMNS['country'], 
        		$this->TABLE_COLUMNS['rankpoints'],
        		$this->TABLE_NAME, 
        		$this->TABLE_COLUMNS['username'], 
        		$search 
        	);
		
        $result = $this->DB_LINK->db_query( $sql );

		if ( empty( $result['error'] ) ) 
		{
			return $result['data'];
		}
		else
		{
			return false;
		}

	}
}

?>